<?php

namespace app\services\plan;
use app\model\course\course\Course;
use app\model\course\course\CourseLinkWare;
use app\model\course\ware\Courseware;
use app\model\plan\course\StudyCourse;
use app\model\plan\PlanCourse;
use app\model\plan\PlanStaff;
use app\services\BaseServices;
class PlanService
{
    public function courseList($planId = 0 , $staffId = 0)
    {
        $courseId = PlanCourse::where('status' , 1)
            ->where('plan_id' , $planId)->column('course_id') ;
        $courseList = Course::field('id,course_name')->where('status' , 1)
            ->where('id' , 'in' , $courseId)->select()->toArray() ;

        if($courseList == []){
            return [] ;
        }

        foreach ($courseList as $k=>$val){
            $coursewareId = CourseLinkWare::where('status' , 1)
                ->where('course_id' , $val['id'])->column('courseware_id') ;
            $wareList = Courseware::field('id,title,hour,topic_open')
                ->whereIn('id' , $coursewareId)->where('status' , 1)->select()->toArray()  ;

            $hourTotal = 0 ;
            $t = 0 ;
            foreach ($wareList as $k2=>$val2){
                $hourTotal = $val2['hour'] + $hourTotal ;

                $studyStaff = StudyCourse::where('status' , 1)
                    ->where('staff_id' ,'=', $staffId)
                    ->where('courseware_id' ,'=', $val2['id'])
                    ->where('plan_id' ,'=', $planId)
                    ->find() ;
                $wareList[$k2]['study_end_status'] = 0 ;
                if($studyStaff){
                    $wareList[$k2]['study_end_status'] = $studyStaff['study_status'] ;
                    $t ++ ;
                }
            }
            $courseList[$k]['sub'] = $wareList ;
            $courseList[$k]['hour_total'] = $hourTotal ;
            $courseList[$k]['ware_total'] = count($wareList) ;
            $courseList[$k]['fold'] = true ;
            $courseList[$k]['study_status'] = 1 ;
            if($t > 0){
                $courseList[$k]['study_status'] = $t == count($wareList) ? 9 : 2 ;
            }
        }
        return $courseList ;
    }
    public function stat($planId = 0 , $staffId = 0 )
    {
        $courseId = PlanCourse::where('status' , 1)
            ->where('plan_id' , $planId)->column('course_id') ;


        $coursewareIds = CourseLinkWare::where('status' , 1)
            ->where('course_id' ,'in', $courseId)
            ->column('courseware_id') ;


        $userTotal = StudyCourse::where('status' , 1)
            ->where('staff_id' ,'=', $staffId)
            ->where('courseware_id' ,'in', $coursewareIds)
            ->where('study_status' ,'=', 9)
            ->where('plan_id' ,'=', $planId)
            ->column('courseware_id') ;


        $hour = Courseware::where('status' , 1)
            ->where('id' ,'in', $coursewareIds)
            ->sum('hour') ;




        $hour2 = Courseware::where('status' , 1)
            ->where('id' ,'in', $userTotal)
            ->sum('hour') ;

        return [
            'stat1' => count($coursewareIds) ,
            'stat2' =>count($userTotal) ,
            'stat3' => $hour ? $hour : 0  ,
            'stat4' =>$hour2 ? $hour2 : 0 ,
        ] ;
    }


    public function progress($planId = 0 , $staffId = 0 )
    {
        $courseId = PlanCourse::where('status' , 1)
            ->where('plan_id' , $planId)->column('course_id') ;

        $coursewareIds = CourseLinkWare::where('status' , 1)
            ->where('course_id' ,'in', $courseId)
            ->column('courseware_id') ;

        $userTotal = StudyCourse::where('status' , 1)
            ->where('staff_id' ,'=', $staffId)
            ->where('courseware_id' ,'in', $coursewareIds)
            ->where('study_status' ,'=', 9)
            ->column('id') ;
        $bfb = sprintf( '%.2f' ,count($userTotal) / count($coursewareIds))*100 ;
        $bfb = $bfb > 100 ? 100 : $bfb ;

        $timeData['progress'] = $bfb ;
        if($bfb >= 100){
            $timeData['study_end_time'] = time() ;
            $timeData['study_status'] = 9 ;
            $timeData['complete_status'] = 9 ;
        }
        PlanStaff::where('staff_id' , $staffId)
            ->where('plan_id' , $planId)
            ->where('status' , 1)
            ->update($timeData);


        return true ;
    }

}